<?php
include_once('config/config.php');

// 当前页
$page = $_GET['page'] ?? 1;

// 每页显示多少条数据
$limit = 10;

// 查询一共有多少条数据
$CountSql = "SELECT COUNT(*) AS `total` FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id`";

$count = find($CountSql);

// 偏移量 limit 从哪里开始(按索引位置) 拿多少条数据
/* 
    1 - 1 = 0 * 10 = 0
    2 - 1 = 1 * 10 = 10 从索引位置为10开始

    1 - 1 = 0
    2 - 1 = 1
*/
$start = ($page - 1) * $limit;

$PersonSql = "SELECT `p`.*,`d`.`name` AS `dname`,`j`.`name` AS `jname` FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id` ORDER BY `createtime` DESC LIMIT $start,$limit";

$PersonData = all($PersonSql);

$PageHtml = ShowPage($page,$count['total'],$limit);

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- 引入公共样式 -->
    <?php include_once('common/meta.php'); ?>
</head>

<body>
    <!-- 引入头部 -->
    <?php include_once('common/header.php'); ?>

    <!-- 引入公共菜单 -->
    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">员工列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">PersonList</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='PersonAdd.php'"><i class="icon-plus"></i>添加员工</button>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>头像</th>
                                <th>姓名</th>
                                <th>性别</th>
                                <th>手机号</th>
                                <th>邮箱</th>
                                <th>部门</th>
                                <th>职位</th>
                                <th>入职时间</th>
                                <th style="width: 26px;"></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($PersonData as $item) {?>
                                <tr>
                                    <td><?php echo $item['id']; ?></td>
                                    <td>
                                        <a href="<?php echo is_file('.' . $item['avatar']) ? $item['avatar'] : '/assets/images/avatar.png'; ?>" target="_blank">
                                            <img src="<?php echo is_file('.' . $item['avatar']) ? $item['avatar'] : '/assets/images/avatar.png'; ?>" style="width: 20px;height:20px;border-radius: 2px;object-fit:cover;" alt="">
                                        </a>
                                    </td>
                                    <td><?php echo $item['name']; ?></td>
                                    <td><?php echo $item['gender'] == 1 ? '男' : ($item['gender'] == 2 ? '女' : '保密'); ?></td>
                                    <td><?php echo $item['mobile']; ?></td>
                                    <td><?php echo $item['email']; ?></td>
                                    <td><?php echo $item['dname']; ?></td>
                                    <td><?php echo $item['jname']; ?></td>
                                    <td><?php echo date('Y-m-d',$item['createtime']); ?></td>
                                    <td>
                                        <a href="PersonEdit.php?id=<?php echo $item['id'];?>"><i class="icon-pencil"></i></a>
                                        <a href="#myModal" data-id="<?php echo $item['id']; ?>" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                                    </td>
                                </tr>
                            <?php }?>
                        </tbody>
                    </table>
                </div>
                <div class="pagination">
                    <?php echo $PageHtml; ?>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">删除</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确认删除该员工？</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button class="btn btn-danger" data-dismiss="modal">删除</button>
                    </div>
                </div>

                <!-- 引入公共底部 -->
                <?php include_once('common/footer.php'); ?>
            </div>
        </div>
    </div>

</body>

</html>

<script>
    let id = 0;

    $('a[href="#myModal"]').click(function(){
        id = $(this).data('id');
    })

    $('.btn-danger').click(function(){
        $.ajax({
            type:'post',
            url:'api.php',
            data:{
                id,
                action:'person_del'
            },
            dataType:'json',
            success:(result) =>
            {
                if(result.code === 1)
                {
                    alert(result.msg);

                    location.href = 'PersonList.php';
                }else{
                    alert(result.msg);
                }
            }
        })
    })
</script>